package com.be.beadmin.project.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.project.dto.input.BatchProductDto;
import com.be.beadmin.project.dto.output.BatchProductEx;
import com.be.beadmin.project.dto.output.BatchProductExcelEx;
import com.be.beadmin.project.service.BatchProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zjm
 * @date 2023-02-13
 **/
@Api(tags = {"项目管理-批次柜体+柜体设计"})
@RestController
public class BatchProductController {

    @Autowired
    private BatchProductService batchProductService;


    @ApiOperation("获取批次柜体分页信息")
    @PostMapping(value = "/project/getBatchProductByPage")
    public ResultModel getBatchProductByPage(@RequestBody PageEntity pageEntity) {
        return ResultModel.success(batchProductService.getBatchProductByPage(pageEntity));
    }

    @ApiOperation("获取所有柜体类型")
    @PostMapping("/project/getProduceType")
    public ResultModel getProduceType() {
        List<String> produceTypeList = batchProductService.getProduceType();
        return ResultModel.success(produceTypeList);
    }

    @ApiOperation("获取所有错误类型")
    @PostMapping("/project/getErroType")
    public ResultModel getErroType() {
        List<String> produceErroList = batchProductService.getErroType();
        return ResultModel.success(produceErroList);
    }

    @ApiOperation("添加一个批次柜体")
    @PostMapping("/project/addBatchProduct")
    public ResultModel addBatchProduct(@RequestBody BatchProductDto batchProductDto) {
        batchProductService.addBatchProduct(batchProductDto);
        return ResultModel.success(ResultModel.ADD);
    }


    @ApiOperation("删除一个批次柜体")
    @PostMapping("/project/delBatchProduct")
    public ResultModel delBatchProduct(@RequestBody BatchProductDto batchProductDto) {
        batchProductService.delBatchProduct(batchProductDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }

    @ApiOperation("修改一个批次柜体")
    @PostMapping("/project/updateBatchProduct")
    public ResultModel updateBatchProduct(@RequestBody BatchProductDto batchProductDto) {
        batchProductService.updateBatchProduct(batchProductDto);
        return ResultModel.success(ResultModel.UPDATE);
    }

    @ApiOperation("获取所有柜体类型及编码的字典")
    @PostMapping("/project/getProductTypeAndCode")
    public ResultModel getProductTypeAndCode() {
        HashMap<String, String> productTypeAndCode = batchProductService.getProductTypeAndCode();
        return ResultModel.success(productTypeAndCode);
    }


    @ApiOperation("导出批次柜体")
    @PostMapping("/project/exportBatchProduct")
    public void exportBatchProduct(HttpServletResponse response, @RequestBody PageEntity pageEntity) throws IOException {
        Page<BatchProductEx> batchProductPage = batchProductService.getBatchProductByPage(pageEntity);
        List<BatchProductEx> batchProductExes = batchProductPage.getContent();
        List<BatchProductExcelEx> batchProductExcelEx = batchProductExes.stream().map(item -> {
                    BatchProductExcelEx excelEx = new BatchProductExcelEx();
                    BeanUtils.copyProperties(item, excelEx);
                    return excelEx;
                }
        ).collect(Collectors.toList());
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("BatchProduct接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
        //EasyExcel.write(response.getOutputStream(), BatchProductExcelEx.class).sheet("sheet1").doWrite(batchProductExcelEx);
        ExcelWriterBuilder ewb=EasyExcel.write(response.getOutputStream(), BatchProductExcelEx.class);
        ewb.excelType(ExcelTypeEnum.XLS);
        ewb.sheet("sheet1").doWrite(batchProductExcelEx);
    }


    @ApiOperation("提交审核")
    @PostMapping("/project/commitDesignReview")
    public ResultModel commitDesignReview(@ApiParam(value = "批次柜体id", required = true) @RequestParam String batchProductId
                                    ) throws Exception {

        batchProductService.commitDesignReview(batchProductId);
        return ResultModel.success("提交审核成功！");

    }
    @ApiOperation("设计经理确认审核,通过=2；驳回=3")
    @PostMapping("/project/confirmDesignReview")
    public ResultModel confirmDesignReview(
                                           @ApiParam(value = "批次柜体id", required = true) @RequestParam String batchProductId,
                                           //@ApiParam(value = "用户id",required = true) @RequestParam String userId,
                                           @ApiParam(value = "审核状态", required = true) @RequestParam int reviewState,
                                           @ApiParam(value = "审核备注") @RequestParam(required = false) String reviewMemo,
                                           @ApiParam(value = "附件") @RequestParam(value="file",required = false) MultipartFile multipartFile
    ) throws Exception {

        batchProductService.confirmDesignReview(batchProductId,reviewState,reviewMemo,multipartFile);
        return ResultModel.success("审核完成！");
    }
    @ApiOperation("批量审核,通过=2；驳回=3")
    @PostMapping("/project/batchConfirmDesignReview")
    public ResultModel batchConfirmDesignReview(@ApiParam(value = "批次柜体id", required = true) @RequestParam List<String> batchProductId,
                                           //@ApiParam(value = "用户id",required = true) @RequestParam String userId,
                                           @ApiParam(value = "审核状态", required = true) @RequestParam int reviewState,
                                           @ApiParam(value = "审核备注") @RequestParam(required = false) String reviewMemo,
                                           @ApiParam(value = "附件") @RequestParam(value="file",required = false) MultipartFile multipartFile
    ) throws Exception {

        batchProductService.batchConfirmDesignReview(batchProductId,reviewState,reviewMemo,multipartFile);
        return ResultModel.success("批量审核完成！");
    }

//    @ApiOperation("设计经理驳回审核")
//    @PostMapping("/project/refuseDesignReview")
//    public ResultModel refuseDesignReview(@ApiParam(value = "批次柜体id", required = true) @RequestParam String batchProductId
//    ) throws Exception {
//
//        batchProductService.refuseDesignReview(batchProductId);
//        return ResultModel.success("已驳回！");
//
//    }



    //柜体设计页面设置工艺路线
    @ApiOperation("设置工艺路线")
    @PostMapping("/project/setBop")
    public ResultModel setBop(@ApiParam(value = "批次柜体id", required = true) @RequestParam String batchProductId,
                              @ApiParam(value = "工艺路线Code", required = true) @RequestParam String bopCode) {

        batchProductService.setBop(batchProductId, bopCode);
        return ResultModel.success("设置成功");
    }

    // 批次计划查询lable后面添加待分配数量，用红色标出
    @ApiOperation("获取柜体待分配数量")
    @PostMapping("/project/getRestProductNums")
    public ResultModel getRestProductNums(@ApiParam(value = "项目id", required = true) @RequestParam String infoId) {



        try {

            return ResultModel.success(batchProductService.getRestProductNums(infoId));

        } catch (Exception e) {
            return ResultModel.error(-2, e.getMessage());
        }

    }

    @ApiOperation("根据柜体编码获取工时对比信息")
    @PostMapping("/project/getWorkingHoursComparisonsByProductCode")
    public ResultModel getWorkingHoursComparisonsByProductCode(@RequestParam String productCode){
        return ResultModel.success("success" , batchProductService.getWorkingHoursComparisonsByProductCode(productCode));
    }

}